//
//  QZHongBaoTixianVC.m
//  shortvideoplay
//
//  Created by tikeios_002 on 2024/7/29.
//

#import "QZHongBaoTixianVC.h"
#import "QZHongBaoTixianHeaderCell.h"
#import "QZHongBaoTixianMoneyCell.h"
#import "CSJADLoading.h"
#import "DJMyWallAcountBindVC.h"

@interface QZHongBaoTixianVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView * tixianCollection;
@property (nonatomic, strong) UIButton * tixianButton;
@property (nonatomic, strong) NSMutableArray * listArr;
@property (nonatomic, strong) NSArray * acountArr;
@property (nonatomic, strong) NSDictionary * integrationDictionary;
@property (nonatomic, assign) CGSize itemSize;
@property (strong, nonatomic)UIView * adView;
@property (strong, nonatomic)NSString * seleteMoneyRow;
@end

@implementation QZHongBaoTixianVC

-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"提现";
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0.0;
    flowLayout.minimumInteritemSpacing = 0.0;
    flowLayout.footerReferenceSize = CGSizeZero;
    flowLayout.headerReferenceSize = CGSizeZero;
    flowLayout.sectionInset = UIEdgeInsetsZero;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    flowLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    CGFloat w = (KScreen_W-50)/3-0.1;
    self.itemSize = CGSizeMake(w, 40);
    //创建collectionView 通过一个布局策略layout来创建
    self.tixianCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KScreen_W, KScreen_H-KSafeAreaBottomHeight-60-KNav_H) collectionViewLayout:flowLayout];
    self.tixianCollection.backgroundColor = viewback_black;
    if (@available(iOS 11.0, *)) {
        self.tixianCollection.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    //代理设置
    self.tixianCollection.scrollsToTop = NO;
    self.tixianCollection.delegate=self;
    self.tixianCollection.dataSource=self;
    self.tixianCollection.pagingEnabled = YES;
    self.tixianCollection.showsVerticalScrollIndicator = NO;
    self.tixianCollection.showsHorizontalScrollIndicator = NO;
    self.tixianCollection.hidden = YES;
    //注册item类型 这里使用系统的类型
    [self.tixianCollection registerNib:[UINib nibWithNibName:@"QZHongBaoTixianHeaderCell" bundle:nil] forCellWithReuseIdentifier:@"QZHongBaoTixianHeaderCell"];
    [self.tixianCollection registerNib:[UINib nibWithNibName:@"QZHongBaoTixianMoneyCell" bundle:nil] forCellWithReuseIdentifier:@"QZHongBaoTixianMoneyCell"];
    
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:self.tixianCollection];

    self.tixianButton = [[UIButton alloc]initWithFrame:CGRectMake(30, KScreen_H-KSafeAreaBottomHeight-55-KNav_H, KScreen_W-60, 50)];
    [self.tixianButton setTitle:@"提现" forState:UIControlStateNormal];
    self.tixianButton.titleLabel.font = Font(15);
    [self.tixianButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.tixianButton addTarget:self action:@selector(tixianClick) forControlEvents:UIControlEventTouchUpInside];
    self.tixianButton.layer.cornerRadius = 10;
    self.tixianButton.backgroundColor = MainColor;
       UILabel * oldshowSSSSTip3 = [[UILabel alloc]init];
    oldshowSSSSTip3.clipsToBounds = YES;
    oldshowSSSSTip3.text = @"kass显示区";
    oldshowSSSSTip3.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip3.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip3.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip3.hidden = YES;
    [self.view addSubview:oldshowSSSSTip3];
    [self.view addSubview:self.tixianButton];
    
    
    
    
    
    _adView = [[UIView alloc]init];
    _adView.frame = CGRectMake(0, 0, KScreen_W, 0);
    [self.view addSubview:_adView];
    KWeakSelf
    [CSJADLoading share].BannerAdsLoadFish = ^(id  _Nullable bannerView) {
        if(bannerView == nil && weakSelf.tixianCollection){
            weakSelf.tixianCollection.frame = CGRectMake(0, 0, KScreen_W, KScreen_H-KSafeAreaBottomHeight-60-KNav_H);
            self.tixianButton = [[UIButton alloc]initWithFrame:CGRectMake(30, KScreen_H-KSafeAreaBottomHeight-55-KNav_H, KScreen_W-60, 50)];
        }else{
            weakSelf.tixianCollection.frame = CGRectMake(0, 0, KScreen_W, KScreen_H-100-KNav_H);
            weakSelf.tixianButton = [[UIButton alloc]initWithFrame:CGRectMake(30, KScreen_H-105-KNav_H, KScreen_W-60, 50)];
            weakSelf.adView.frame = CGRectMake(0, KScreen_H-45-KNav_H, KScreen_W, 45);
            [weakSelf.adView addSubview:bannerView];
            [bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(weakSelf.adView);
            }];
            [UIView animateWithDuration:0.3 animations:^{
                [weakSelf.view layoutIfNeeded];
            }];
        }
        
    };
    [CSJADLoading ADBannerAdRequest:self];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self activityTaskListRequest];
}
- (NSMutableArray *)listArr{
    if(_listArr == nil){
        _listArr = [NSMutableArray new];
    }
    return _listArr;
}
- (void)activityTaskListRequest{
    KMutableDic(sendDic)
    KWeakSelf
    [YBNetBase integrationWalletListMoneyRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            weakSelf.integrationDictionary = data[@"integration_account"];
            weakSelf.acountArr = data[@"accountData"];
            [weakSelf.listArr removeAllObjects];
            [weakSelf.listArr addObjectsFromArray:data[@"moneyData"]];
            [weakSelf.tixianCollection reloadData];
            weakSelf.tixianCollection.hidden = NO;
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
#pragma mark uitableview delege
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(section == 0){
        return 1;
    }else{
        return self.listArr.count;
    }
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        QZHongBaoTixianHeaderCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QZHongBaoTixianHeaderCell" forIndexPath:indexPath];
        cell.sumXianjinLabel.text = [NSString stringWithFormat:@"%@元",self.integrationDictionary[@"normal_amount"]];
        if(self.acountArr == nil || self.acountArr.count == 0){
            cell.acountNameLabel.text = @"未设置提现账号";
        }else{
            cell.acountNameLabel.text = self.acountArr[0][@"accounts"];
        }
        KWeakSelf
        cell.QZHongBaoTixianHeaderBlock = ^{
            DJMyWallAcountBindVC * bind = [[DJMyWallAcountBindVC alloc]init];
            if(self.acountArr.count > 0){
                bind.accountDic = self.acountArr[0];
            }
            [weakSelf.navigationController pushViewController:bind animated:YES];
        };
        return cell;
    }else{
        QZHongBaoTixianMoneyCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QZHongBaoTixianMoneyCell" forIndexPath:indexPath];
        if(self.seleteMoneyRow.length != 0 && [self.seleteMoneyRow intValue] == indexPath.row){
            cell.backgroundColor = MainColor;
        }else{
            cell.backgroundColor = subviewback_black;
        }
        cell.numberLabel.text = [NSString stringWithFormat:@"%@元",self.listArr[indexPath.row][@"money"]];
        return cell;
    }
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if(section == 0){
        return 0;
    }else{
        return 10;
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if(section == 0){
        return 0;
    }else{
        return 10;
    }
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
 layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section
{
    if(section == 0){
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }else{
        return UIEdgeInsetsMake(0, 15, 0, 15);
    }
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        return CGSizeMake(KScreen_W, 140);
    }else{
        return self.itemSize;
    }
}
- (void)showTextViewSubViewToViewTo1:(UIView * )subView{

    UIView * showTextViewSubViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewSubViewTo1.clipsToBounds = YES;
    [subView addSubview:showTextViewSubViewTo1];
    UILabel * showTextViewSubViewLabelTo2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    showTextViewSubViewLabelTo2.text = @"56464645";
    [showTextViewSubViewTo1 addSubview:showTextViewSubViewLabelTo2];
    
    UIImageView * showTextViewSubViewTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [subView addSubview:showTextViewSubViewTo2];

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        
        
    }else{
        _seleteMoneyRow = [NSString stringWithFormat:@"%ld",indexPath.row];
        [collectionView reloadData];
    }
}
-(void)tixianClick{
    KMutableDic(sendDic)
    if(_seleteMoneyRow.length == 0){
        [YBMsgPop showPop:@"请选择提现金额"];
        return;
    }
    if(self.acountArr.count == 0){
        [YBMsgPop showPop:@"请绑定提现账户"];
        return;
    }
    NSInteger index = [_seleteMoneyRow intValue];
    sendDic[@"money"] = self.listArr[index][@"money"];
    sendDic[@"accountid"] = [NSString stringWithFormat:@"%@",self.acountArr[0][@"id"]];
    KWeakSelf
    [YBNetBase integrationWalletListCashRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            [YBMsgPop showPop:msg];
            [weakSelf activityTaskListRequest];
            if(weakSelf.QZHongBaoTixianBlock){
                weakSelf.QZHongBaoTixianBlock();
            }
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}

@end
